function uploaderCreate(fieldId, multi){
    var post = new Object;
    var url;
    post['fieldId'] = fieldId;
    $(document).ready(function(){
        if(fieldId == 'global' || $('#uploadify-'+fieldId+'Uploader').length == 0)
        $('input#uploadify-'+fieldId).uploadify({
            'uploader'  : 'modules/uploadify/uploadify.swf',
            'script'    : 'modules/uploadify/uploadify.php',
            'cancelImg' : 'modules/uploadify/cancel.png',
            'auto'      : false,
            'folder'    : "../upload",
            'multi'     : multi == 1 ? true : false,
            'onSelectOnce' : function() {
                                url =  "./file.php?action=upload";
                                if(fieldId != 'global'){
                                    if($("input#itemId").val() == "") createNewItem();
                                    post['itemId'] = $("input#itemId").val();
                                }
                                post['size'] = new Object;
                                post['itemId'] = $("input#itemId").val();
                                post['size']['s'] = $('select#sizeS-'+fieldId+' :selected').val();
                                if($('input#sizeBStatus-'+fieldId).is(':checked') == true) post['size']['b']= $('select#sizeB-'+fieldId+' :selected').val();
                                $('input#uploadify-'+fieldId).uploadifyUpload();
                        },
            'onComplete': function(event, queueID, fileObj, response, data){
                                    post["fileName"]= fileObj.name;
                                    $.post(url,post, function(response){
                                        if(response.status == 'OK!'){
                                            if(response.relation == 3) $('.filesList#tab-'+fieldId+'-'+response.file.fileType).append(response.content);
                                            else if (response.relation == 1) $('#upload-file-wraper-'+fieldId).append(response.content);
                                        }
                                    }, 'json');
                        }
        });
    });
}

function filesRead(fieldId){
    var url = "./index.php?type=file&action=read";
    var itemId = $("input#itemId").val();
    var post = {"itemId":(itemId ? itemId :$("input#itemId").val()), "fieldId":fieldId};
    $.post(url,post,function(response){
        $("#"+fieldId+"_list").html(response);
    });
}

function fileDelete(id, fileType, fieldId){
    var url = "./file.php?action=delete";
    var post = {'id': id, 'fieldId':fieldId};
    $.post(url,post,function(){
        $('.filesList#tab-'+fieldId+'-'+fileType+' li#fileBox-'+id).remove();
    });
}

function sizeStatus(fieldId, type){
    if($('input#size'+type+'Status-'+fieldId).is(':checked') == false) $('select#size'+type+'-'+fieldId).css('display', 'none');
    else $('select#size'+type+'-'+fieldId).css('display', 'inline');
}

function imageRotate(id, fileType, fieldId){
    var url = "./file.php?action=imageRotate";
    var post = {'id': id, 'fieldId':fieldId};
    $.post(url,post,function(response){
        $('.filesList#tab-'+fieldId+'-'+fileType+' li#fileBox-'+id).replaceWith(response);
    });
}

function createDialog(action, fieldId, fileId){
    var post = new Object;
    post['action'] = action;
    post['fieldId'] = fieldId;
    post['fileId'] = fileId;
    var url = './dialog.php?action=create';
    $.post(url, post,function(response){
        if(response.status == 'OK!'){
            $('#fieldContent-'+fieldId).append(response.content);
            $("#dialog-"+fieldId).dialog(
            {
                autoOpen: true,
                height: 'auto',
                width: 'auto',
                resizable: false,
            
            });
        }
        else alert('Error:'+response.content);
    },'json');
}

function setFileAlt(fieldId, id){
    var url = "./file.php?action=setFileAlt";
    var post = {'fieldId': fieldId, 'id':id, 'alt':$('#fileAlt-'+fieldId).val()};
    $.post(url,post);
}